/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import offset.nodes.client.editor.model.EditorDocument;

/**
 *
 * @author  Walter Lütgenau
 */
public class StyleClassDialog extends javax.swing.JDialog {

    EditorDocument document;
    public static final int RET_CANCEL = 0;
    public static final int RET_OK = 1;
    protected int returnStatus;
    protected String selectedStyleClassName;
    ArrayList styles = new ArrayList();

    /** Creates new form StyleClassDialog */
    public StyleClassDialog(java.awt.Frame parent, boolean modal, EditorDocument editor) {
        super(parent, modal);
        this.document = editor;

        initComponents();
        updateStyles();

        styleList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                int index = styleList.getSelectedIndex();
                selectedStyleClassName = (String) styles.get(index);
            }
        });

        styleList.addMouseListener(new DoubleClickAdapter());

        getRootPane().setDefaultButton(okButton);
    }

    class DoubleClickAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getClickCount() == 2)
                doClose(RET_OK);
        }
    }

    protected void updateStyles() {
        styles.clear();
        Enumeration i = document.getStyleSheet().getStyleNames();
        while (i.hasMoreElements()) {
            String styleName = (String) i.nextElement();
            System.out.println("Style: " + styleName);
            if (classRadioButton.isSelected()) {
                if (styleName.startsWith(".") && styleName.indexOf(":") < 0 && styleName.indexOf(" ") < 0)
                    styles.add(styleName.substring(1));
            } else
                if (styleName.startsWith("#") && styleName.indexOf(":") < 0 && styleName.indexOf(" ") < 0)
                    styles.add(styleName.substring(1));

            Collections.sort(styles);
            styleList.setModel(new StyleSheetListModel(styles));
        }
    }

    class StyleSheetListModel extends AbstractListModel {

        List styles;

        public StyleSheetListModel(List styles) {
            this.styles = styles;
        }

        public Object getElementAt(int index) {
            return styles.get(index);
        }

        public int getSize() {
            return styles.size();
        }
    }

    public String getSelectedStyleClassName() {
        return selectedStyleClassName;
    }

    public boolean isClassStyle() {
        return classRadioButton.isSelected();
    }

    public boolean isIdStyle() {
        return idRadioButton.isSelected();
    }

    public int getReturnStatus() {
        return returnStatus;
    }

    protected void doClose(int returnStatus) {
        this.returnStatus = returnStatus;
        setVisible(false);
        dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        styleTypeButtonGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        styleList = new javax.swing.JList();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        classRadioButton = new javax.swing.JRadioButton();
        idRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        styleList.setModel(new StyleSheetListModel(styles));
        jScrollPane1.setViewportView(styleList);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        okButton.setText(bundle.getString("Button.ok")); // NOI18N
        okButton.setPreferredSize(new java.awt.Dimension(67, 23));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButton(evt);
            }
        });

        cancelButton.setText(bundle.getString("Button.cancel")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButton(evt);
            }
        });

        styleTypeButtonGroup.add(classRadioButton);
        classRadioButton.setSelected(true);
        classRadioButton.setText(bundle.getString("styles.classes")); // NOI18N
        classRadioButton.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                setClassType(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        styleTypeButtonGroup.add(idRadioButton);
        idRadioButton.setText(bundle.getString("styles.ids")); // NOI18N
        idRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setIdType(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(classRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(idRadioButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE))))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idRadioButton)
                    .addComponent(classRadioButton))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButton(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButton
        doClose(RET_OK);
    }//GEN-LAST:event_okButton

    private void cancelButton(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButton
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButton

    private void setClassType(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_setClassType
        updateStyles();
    }//GEN-LAST:event_setClassType

    private void setIdType(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setIdType
        updateStyles();
    }//GEN-LAST:event_setIdType
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                StyleClassDialog dialog = new StyleClassDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JRadioButton classRadioButton;
    private javax.swing.JRadioButton idRadioButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JList styleList;
    private javax.swing.ButtonGroup styleTypeButtonGroup;
    // End of variables declaration//GEN-END:variables
}
